package com.neighbor.neighborhoodadmin.controller;

import com.neighbor.neighborhoodadmin.pojo.Notifications;
import com.neighbor.neighborhoodadmin.pojo.dto.NotificationDTO;
import com.neighbor.neighborhoodadmin.service.INotificationsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 * 系统通知表 前端控制器
 * </p>
 *
 * @author song
 * @since 2025-09-22 09:11:23
 */
@RestController
@RequestMapping("/admin/notifications")
public class NotificationsController {

    private final INotificationsService notificationsService;

    @Autowired
    public NotificationsController(INotificationsService notificationsService) {
        this.notificationsService = notificationsService;
    }

    @GetMapping
    public ResponseEntity<Map<String, Object>> getAllNotifications(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size) {
        System.out.println("page:"+page+"size"+size);
        Map<String, Object> result = notificationsService.getAllNotifications(page, size);
        return ResponseEntity.ok(result);
    }

    @GetMapping("/{id}")
    public ResponseEntity<Notifications> getNotificationById(@PathVariable Integer id) {
        Notifications notification = notificationsService.getNotificationById(id);
        if (notification == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(notification);
    }

    @PostMapping
    public ResponseEntity<Integer> createNotification(@RequestBody NotificationDTO dto) {
        dto.setType((byte)1);
        int rows = notificationsService.createNotification(dto);
        return new ResponseEntity<>(rows, HttpStatus.CREATED);
    }

    @PutMapping("/{id}")
    public ResponseEntity<Integer> updateNotification(
            @PathVariable Integer id,
            @RequestBody NotificationDTO dto) {
        int rows = notificationsService.updateNotification(id, dto);
        return rows > 0 ? ResponseEntity.ok(rows) : ResponseEntity.notFound().build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Integer> deleteNotification(@PathVariable Integer id) {
        int rows = notificationsService.deleteNotification(id);
        return rows > 0 ? ResponseEntity.noContent().build() : ResponseEntity.notFound().build();
    }
}
